var app = getApp();
Page({

  data: {
    userInfo: null,
    money:0,
    extract_money:0
  },

  onLoad: function (options) {
      var that = this;
      app.request("/user/getMoney", function (data, ret) {
          that.setData({ money: data.money });
      }, function (data, ret) {
          app.error(ret.msg);
      });
      this.loadArchives();
  },

  onShow: function () {
    if (!app.globalData.userInfo) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }
  },
    goDetail: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../news/detail?id='+id
        })
    },
    vote: function (event) {
        var that = this;
        var archivesIndex = event.currentTarget.dataset.index;

        app.vote(event, function (data) {
            that.data.archivesList[archivesIndex].likes = data.likes;
            that.data.archivesList[archivesIndex].is_likes = 1;
            that.setData({ ['likes']:  1,'archivesList':that.data.archivesList});
        });
    },
    collection: function (event) {

        var that = this;
        var archivesIndex = event.currentTarget.dataset.index;

        app.collections(event, function (data) {
            that.data.archivesList[archivesIndex].collection = data.collection;
            that.setData({ ['collections']: 1,'archivesList':that.data.archivesList});
        });
    },
    /**
     * 弹窗
     */
    showDialogBtn: function (event) {
        this.setData({
            showModal: true
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {
    },
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
        this.setData({
            showModal: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
        this.hideModal();
    },
    /**
     * 获取评论内容
     */
    getPinglun: function (e) {
        var val = e.detail.value;
        this.setData({
            extract_money: val
        });
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function (event) {
        var that = this;
        var m = that.data.money;
        var em = that.data.extract_money;

        if (em == '') {
            app.error('请输入提现金额');
            return;
        }

        if (em > m) {
            app.error('可提现余额不足');
            return;
        }

        app.request("/order/extractSubmit", {extract_money:em }, function (data, ret) {
            console.log(data)
        }, function (data, ret) {
            app.error(ret.msg);
        });

        this.hideModal();
    },
    loadArchives: function (cb) {
        var that = this;
        if (that.data.nomore == true || that.data.loading == true) {
            return;
        }
        this.setData({ loading: true });
        app.request('/archives/index', { model: 0,channel: 45, page: this.page,selectedId:0}, function (data, ret) {
            var vote = wx.getStorageSync("vote") || [];
            for (var index in data.archivesList) {
                if (vote.indexOf(data.archivesList[index].id)>-1){
                    data.archivesList[index].is_likes = 1;
                }
            }
            that.setData({
                loading: false,
                nodata: that.page == 1 && data.archivesList.length == 0 ? true : false,
                nomore: that.page > 1 && data.archivesList.length == 0 ? true : false,
                archivesList: that.page > 1 ? that.data.archivesList.concat(data.archivesList) : data.archivesList,
                tabList2: data.tabList2,
                tabList2_count:data.tabList2_count,
            });
            that.page++;
            typeof cb == 'function' && cb(data);
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },

})